<?php
declare (strict_types = 1);
namespace app\model\tablenot\tool;

use think\facade\Cache;

class RedisService
{
    protected $redis;
    protected $options;
    protected $key;
    function __construct()
     {
         $this->redis = Cache::store('redis');
     }

     //不用ins 名称会冲突
    static $stance = false;

    /**
     * @return bool|RedisService
     */
    public static function instance()
    {
        if(!self::$stance){
            self::$stance =  new self();
        }
        return self::$stance;
    }

    public function getRedis()
    {
        return $this->redis;
    }

    /**
     * 获取redis指定key
     * @param $key
     * @return mixed
     */
    public function keys($key)
    {
        return $this->redis->keys($key);
    }

    public function hMGet($key,$arr){
       return $this->redis->hMget($key,$arr);
    }

    public function blpop($key,$n){
        return $this->redis->blpop($key,$n);
    }

    /**
     * select db
     * @param $db
     * @return mixed
     */
    public function select($db){
        return $this->redis->select($db);
    }
    /**
     * set
     * @param $k
     * @param $v
     * @param null $expire
     * @return mixed
     */
    public function set($k,$v,$expire = null){
        if($expire == null){
            return $this->redis->set($k,$v);
        }else{
            $expire = (int)$expire;
            return $this->redis->set($k,$v,$expire);
        }
    }

    /**
     * setnx
     * @param $k
     * @param $v
     * @return mixed
     */
    public function setnx($k,$v){
        return $this->redis->setnx($k,$v);
    }

    /**
     * incrBy
     * @param $k
     * @param $v
     * @return mixed
     */
    public function incrBy($k,$v){
        return $this->redis->incrBy($k,$v);
    }

    /**
     * 设置过期时间且不被更新
     * @param $k
     * @param $ttl
     * @return mixed
     */
    public function expireat($k,$ttl){
        return $this->redis->Expireat($k,$ttl);
    }
    /**
     * get
     * @param $k
     * @return mixed
     */
    public function get($k){
        return $this->redis->get($k);
    }

    /**
     * get
     * @param $k
     * @return mixed
     */
    public function ttl($k){
        return $this->redis->ttl($k);
    }


    /**
     * del
     * @param $k
     * @return mixed
     */
    public function del($k){
        return $this->redis->del($k);
    }
    /**
     * 存哈希
     * @param $k
     * @param $hashkey
     * @param $v
     * @return mixed
     */
    public function hSet($k,$hashkey,$v){
        return $this->redis->hset($k,$hashkey,$v);
    }

    /**
     * 取哈希
     * @param $k
     * @param $hashkey
     * @return mixed
     */
    public function hGet($k,$hashkey){
        return $this->redis->hGet($k,$hashkey);
    }

    /**
     * 为哈希表 key 中的指定字段的整数值加上增量
     * @param $k
     * @param $hashkey
     * @param $v
     * @return mixed
     */
    public function hIncrBy($k,$hashkey,$v){
        return $this->redis->hIncrBy($k,$hashkey,$v);
    }

    /**
     * 为哈希表 key 中的指定字段的浮点数值加上增量
     * @param $k
     * @param $hashkey
     * @param $v
     * @return mixed
     */
    public function hIncrByFloat($k,$hashkey,$v){
        return $this->redis->hIncrByFloat($k,$hashkey,$v);
    }

    /**
     * 求哈希总数
     * @param $k
     * @return mixed
     */
    public function hLen($k){
        return $this->redis->hLen($k);
    }

    /**
     * 删除哈希
     * @param $k
     * @return mixed
     */
    public function hDel($k,$hashkey){
        return $this->redis->hDel($k,$hashkey);
    }

    /**
     * 模糊删除删除哈希
     * @param $key
     * @param $hashkey 模糊删除的key
     * @return mixed
     */
    public function hDelFuzzy($key,$hashkey)
    {
        $keys = $this->redis->hKeys($key);
        foreach ($keys as $val){
            if(strpos($val,$hashkey) !== false){
                $this->hDel($key,$val);
            }
        }
        return true;
    }

    /**
     * 列出哈希总数量
     * @param $k
     * @return mixed
     */
    public function hGetAll($k){
        return $this->redis->hGetAll($k);
    }
    /**
     * 迭代获取
     * @param $k
     * @return mixed
     */
    public function hScan($k,$iterator){
        return $this->redis->hScan($k,$iterator);
    }

    /**
     * 无序集合添加
     * @param $k
     * @param $val
     * @return bool|int
     */
     public  function sAdd($k,$val){

         return $this->redis->sAdd($k,$val);
    }
    /**
     * 无序集合迭代获取
     * @param $k
     * @param $val
     * @return bool|int
     */
    public  function sScan($k,$it){

        return $this->redis->sScan($k,$it);
    }
    /**
     * 弹出并删掉该条记录
     * @param $k
     * @return mixed
     */
    public function spop($k){
         return $this->redis->spop($k);
    }

    /**
     * 无序集合取成员值
     * @param $k
     * @return array
     */
    public function sMembers($k){
        return $this->redis->sMembers($k);
    }

    /**
     * 移除一个元素
     * @param $k
     * @param $v
     * @return mixed
     */
    public function srem($k,$v){
        return $this->redis->sRem($k,$v);
    }
    /**
     * 有序集合添加
     * @param $k
     * @param $options
     * @param $score
     * @return int
     */
    public function zAdd($k, $score, $options){
        return $this->redis->zAdd($k,$score,$options);
    }

    /**
     * 有序集合获取分数
     * @param $k
     * @param $member
     * @return bool|float
     */
    public function zscore($k, $member){
        return $this->redis->zScore($k, $member);
    }

    /**
     * 通过索引区间返回有序集合指定区间内的成员
     * @param $k
     * @param $start
     * @param $end
     * @param null $withscores
     * @return array
     */
    public function zrange($k,$start, $end, $withscores = null){
        return $this->redis->zRange($k, $start, $end, $withscores = null);
    }

    /**
     * 计算有序集合中指定分数区间的成员数量
     * @param $k
     * @param $start
     * @param $end
     * @return int
     */
    public function zcount($k,$start, $end){
        return $this->redis->zCount($k, $start, $end);
    }

    /**
     * 对有序集合中指定成员的分数加上增量increment
     * @param $k
     * @param $increment
     * @param $member
     * @return int
     */
    public function zincrby($k,$increment, $member){
        return $this->redis->zIncrby($k,$increment,$member);
    }

    /**
     * 根据分数获取有序集合成员
     * @param $k
     * @param $start
     * @param $end
     * @param array $options
     * @return mixed
     */
    public function zrangebyscore($k, $start, $end, array $options = array()){
        return $this->redis->zRangeByScore($k, $start, $end, $options = array());
    }

    /**
     * 根据成员获取有序集合数量
     * @param $k
     * @param $start
     * @param $end
     * @return mixed
     */
    public function zLexCount($k, $start, $end){
        return $this->redis->zLexCount($k, $start, $end);
    }
    /**
     * 删除有序集合中的成员
     * @param $k
     * @param $member1
     * @return int
     */
    public function zrem($k, $member1){
        return $this->redis->zRem($k, $member1);
    }

    /**
     * 队列导入
     * @param $k
     * @param $v
     * @return mixed
     */
    public function lpush($k,$v){
        return $this->redis->lpush($k,$v);
    }

    public function lrange($k, $start, $stop)
    {
        return $this->redis->lrange($k,$start, $stop);
    }

    public function ltrim($k, $start, $stop)
    {
        return $this->redis->ltrim($k,$start, $stop);
    }

    /**
     * 获取队列的长度
     * @param $k
     * @return mixed
     */
    public function llen($k){
        return $this->redis->llen($k);
    }
    /**
     * 出队列
     * @param $k
     * @return mixed
     */
    public function rpop($k){
        return $this->redis->rpop($k);
    }
}